<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>基础的get请求</title>
</head>

<body>
  <h1>基础的get请求</h1>
  <form id="form" action="" method="">
    <input id="ipt" type="text">
    <button id="btn">输入学生id查询详细信息</button>
  </form>


  <script>
    window.onload = function () {
      const obtn = document.getElementById('btn')
      const oipt = document.getElementById('ipt')
      const oform = document.getElementById('form')
      // 给表单绑定提交事件
      oform.onsubmit = function () {
        const userId=oipt.value
        // 1实例化XMLHttpRequest
        const xhr = new XMLHttpRequest()
        // 2打开路径
        // 参数1 请求方式 2 请求地址 3 是否异步
        xhr.open("GET", `/userInfo/${userId}?&_=${Date.now()}`, true)
        // 3发送请求
        xhr.send()
        // 4 监听事件
        xhr.onreadystatechange = function () {
          if (xhr.readystate === 4 && (xhr.status >= 200 && xhr.status <= 299)) {
            console.log(JSON.parse(xhr.response));
          }
        }
        return false
      }


    }
  </script>


</body>

</html>